import Vue from 'vue'

/**
 * promise形式    getSetting
 * @returns {Promise<unknown>}
 */
export const getSetting = () => {
	return new Promise((resolve, reject) => {
		uni.getSetting({
			success: (result) => {
				resolve(result)
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}
/**
 * promise形式    chooseAddress
 * @returns {Promise<unknown>}
 */
export const chooseAddress = () => {
	return new Promise((resolve, reject) => {
		uni.chooseAddress({
			success: (result) => {
				resolve(result)
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

/**
 * promise形式    openSetting
 * @returns {Promise<unknown>}
 */
export const openSetting = () => {
	return new Promise((resolve, reject) => {
		uni.openSetting({
			success: (result) => {
				resolve(result)
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}
/**
 * promise形式    showModal
 * @param title     弹窗标题
 * @param content   弹窗内容
 * @returns {Promise<unknown>}
 */
export const showModal = ({
	title,
	content
}) => {
	return new Promise((resolve, reject) => {
		uni.showModal({
			title: title,
			content: content,
			success: (res) => {
				if (res.confirm) {
					resolve(res)
				} else {
					reject(res)
				}
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

/**
 * promise形式    showToast
 * @param title     提示内容
 * @returns {Promise<unknown>}
 */
// export const showToast = (title,icon,duration) => {
//   return new Promise((resolve, reject) => {
//       uni.showToast({
//           title: title,
//           icon: icon || "none",
// 					duration:duration || 2000,
//           success: (result) => {
//               resolve(result)
//           },
//           fail: (err) => {
//               reject(err)
//           }
//       })
//   })
// }


export const showToast = (title, icon, duration) => {


	// #ifdef APP-PLUS
	
	//原生打包的时候使用
			
	//@"alertFail" @"alertOK" @"alertTan"
	// uni.sendNativeEvent('alertView', {
	// 	title: title,
	// 	alertImg: icon || 'alertFail',
	// }, ret => {})
		
	//HbuildderX直接调试的时候使用
	setTimeout(()=>{
		let type = 'short'
		if (duration && duration > 2) {
			type = 'long'
		}
		plus.nativeUI.toast(title, {
			align: 'center',
			verticalAlign: 'center',
			duration: type
		});
		console.log('App吐丝')			
	},200)

	// #endif

	// #ifndef APP-PLUS
	uni.showToast({
		title: title,
		icon: icon || "none",
		duration: duration || 2000
	})
	console.log('H5吐丝')
	// #endif
}



/**
 * promise形式    login
 * @returns {Promise<unknown>}
 */
export const login = () => {
	return new Promise((resolve, reject) => {
		uni.login({
			timeout: 10000,
			success(result) {
				resolve(result)
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

/**
 * promise形式    requestPayment  微信支付
 * @param pay   后台传递参数,解构
 * @returns {Promise<unknown>}
 */
export const requestPayment = (pay) => {
	return new Promise((resolve, reject) => {
		uni.requestPayment({
			...pay,
			success(res) {
				resolve(resolve)
			},
			fail(err) {
				reject(err)
			}
		})
	})
}
Vue.prototype.$toast = showToast
